package tests.cases {
	
	import mx.rpc.AsyncToken;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	import org.flexunit.Assert;
	
	import tests.cases.DefaultTestCase;
	
	public class TestLogin extends DefaultTestCase {
		
		private var coreSystemService:RemoteObject;
		
		[Before]
		public function setUp():void {
			
			coreSystemService = new RemoteObject();
			coreSystemService.destination = "amfphp";
			coreSystemService.source = "CoreSystemService";
		}
		
		[After]
		public function tearDown():void {
			
			coreSystemService = null;
		}
		
		// ----------------------------------------------------------------
		// TEST GOOD LOGIN
		// ----------------------------------------------------------------
		[Test(async)]
		public function testGoodLogin():void {
			
			var argsVO:Object = {
				email: "admin@test.com",
				password: "password"
			};
			
			var token:AsyncToken = coreSystemService.loginUser(argsVO.email, argsVO.password);
			runAsyncTest(token, argsVO, handleGoodLoginSuccess);
		}
		
		private function handleGoodLoginSuccess(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNotNull(message.result);
			
			Assert.assertNotNull(message.result.sessionID);
			Assert.assertNotNull(message.result.userData);
			Assert.assertNotNull(message.result.groups);
			Assert.assertNotNull(message.result.theme);
			Assert.assertNotNull(message.result.avatar);
			
			Assert.assertEquals("user001", message.result.userData.userUID);
		}
		
		// ----------------------------------------------------------------
		// TEST BAD LOGIN
		// ----------------------------------------------------------------
		[Test(async)]
		public function testBadLogin():void {
			
			var argsVO:Object = {
				email: "bad",
				password: "login"
			};
			
			var token:AsyncToken = coreSystemService.loginUser(argsVO.email, argsVO.password);
			runAsyncTest(token, argsVO, handleBadLoginSuccess);
		}
		
		private function handleBadLoginSuccess(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNull(message.result);
		}
	}
}